<!DOCTYPE html>
<html>
  <head><meta name="generator" content="Hexo 3.8.0">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="fengzhaoyang&#39;s blog">
  <meta name="keyword" content="hexo-theme, vuejs">
  
  <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
    springboot 注解 | fzy-blog
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  <link rel="stylesheet" href="/css/style.css">
  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
  <script src="/js/qrious.js"></script>
  
  
  
  
    <!-- MathJax support START -->
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <!-- MathJax support END -->
  


</head>
<div class="wechat-share">
  <img src="/css/images/logo.png">
</div>
  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>fzy-blog</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">Home</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">Tags</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">Archives</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/project/" class="item-link">Projects</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">About</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">Home</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">Tags</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">Archives</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/project/" class="menu-link">Projects</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">About</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>springboot 注解</h2>
  <p class="post-date">2019-05-24</p>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h1 id="springboot-注解"><a href="#springboot-注解" class="headerlink" title="springboot 注解"></a>springboot 注解</h1><p>Bean 声明注解<br>@Service 业务逻辑层</p>
<p>@Component 组件</p>
<p>@Repository 数据访问层</p>
<p>@Controller Spring mvc 展现层</p>
<p>@Configurable 声明当前类是一个配置类</p>
<p>@ComponentScan(“com.weibo.ad”)</p>
<p>设置自动扫描包下面所有的 @Service @Component @Repository @Controller<br>@EnableAsync</p>
<p>开启异步任务支持<br>@PropertySource(“classpath:demo.properties”)</p>
<p>@Component<br>@PropertySource(“classpath:demo.properties”)<br>public class Demo {<br>@Value(“\${test.name}”)<br>private String testName;<br>}<br>@EnableScheduling 注解开启对计划任务的支持</p>
<p>Bean 注入注解</p>
<p>@Autowired Spring 提供</p>
<p>@Resource JSR-250</p>
<p>@Value(“xxxx”) 注入普通字符串</p>
<p>@Value(“\${xxx.xxx}”) 注入配置文件中字符串</p>
<p>@PostConstruct 标注在方法上，在构造函数执行完毕后执行</p>
<p>@PreDestroy Bean 标注在方法上，销毁前执行</p>
<p>@Async 异步方法表明，若是在 class 上则全是</p>
<p>@Scheduled 声明方法是计划任务</p>
<p>@Conditional() 条件注解，当满足某条件时</p>
<p>Spring MVC<br>@RequestMapping</p>
<p>用于映射 Web 请求 返回体，编码格式都可以在此处设置 produces = {} 设置返回值 json/xml charset 等<br>@RestController</p>
<p>这是个组合注解，组合了@Controller 和@ResponseBody<br>@ResponseBody</p>
<p>支持返回体放入 response 体中，而不是直接返回一个页面， 此注解可以放在返回值或者方法体上<br>@RequestBody</p>
<p>允许参数在 request 体里，而不是在地址栏后面<br>@PathVariable</p>
<p>用来接收路径参数，api/{id}</p>
<ol>
<li>@Component 是所有受 Spring 管理组件的通用形式，@Component 注解可以放在类的头上，@Component 不推荐使用。</li>
<li>@Repository</li>
<li>Service</li>
<li><p>@Controller 对应表现层的 Bean，也就是 Action<br>@Scope(“prototype”)表示将 Action 的范围声明为原型<br>@Scope(“singleton”) spring 默认 scope 是单例模式(scope=“singleton”)</p>
</li>
<li><p>指定注解扫描多个包（多个包以逗号隔开）<br>&lt;context:component-scan base-package=“cn.gacl.dao.impl,cn.gacl.service.impl,cn.gacl.action”/&gt;</p>
</li>
</ol>
<p>@Configuration 把一个类作为一个 IoC 容器，它的某个方法头上如果注册了@Bean，就会作为这个 Spring 容器中的 Bean。</p>
<p>@Scope 注解 作用域</p>
<p>@Lazy(true) 表示延迟初始化</p>
<p>@Service 用于标注业务层组件、</p>
<p>@Controller 用于标注控制层组件（如 struts 中的 action）</p>
<p>@Repository 用于标注数据访问组件，即 DAO 组件。</p>
<p>@Component 泛指组件，当组件不好归类的时候，我们可以使用这个注解进行标注。</p>
<p>@Scope 用于指定 scope 作用域的（用在类上）</p>
<p>@PostConstruct 用于指定初始化方法（用在方法上）</p>
<p>@PreDestory 用于指定销毁方法（用在方法上）</p>
<p>@DependsOn：定义 Bean 初始化及销毁时的顺序</p>
<p>@Primary：自动装配时当出现多个 Bean 候选者时，被注解为@Primary 的 Bean 将作为首选者，否则将抛出异常</p>
<p>@Autowired 默认按类型装配，如果我们想使用按名称装配，可以结合@Qualifier 注解一起使用。如下：</p>
<p>@Autowired @Qualifier(“personDaoBean”) 存在多个实例配合使用</p>
<p>@Resource 默认按名称装配，当找不到与名称匹配的 bean 才会按类型装配。</p>
<p>@PostConstruct 初始化注解</p>
<p>@PreDestroy 摧毁注解 默认 单例 启动就加载</p>
<p>@Async 异步方法调用</p>
</section>
    <!-- Tags START -->
    
    <div class="tags">
      <span>Tags:</span>
      
  <a href="/tags#SpringBoot2">
    <span class="tag-code">SpringBoot2</span>
  </a>

    </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/2019/05/24/SpringBoot2/springboot整合Shiro实现单点登录/">
        <span class="nav-arrow">← </span>
        
          spring boot 整合 Shiro 实现单点登录
        
      </a>
    
    
      <a class="nav-right" href="/2019/05/24/SpringBoot2/springbootmybatis动态数据源配置/">
        
          springboot mybatis 动态数据源配置
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
    <div class="money-like">
      <div class="reward-btn">
        赏
        <span class="money-code">
          <span class="alipay-code">
            <div class="code-image"></div>
            <b>使用支付宝打赏</b>
          </span>
          <span class="wechat-code">
            <div class="code-image"></div>
            <b>使用微信打赏</b>
          </span>
        </span>
      </div>
      <p class="notice">若你觉得我的文章对你有帮助，欢迎点击上方按钮对我打赏</p>
    </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
    <div class="qrcode">
      <canvas id="share-qrcode"></canvas>
      <p class="notice">扫描二维码，分享此文章</p>
    </div>
    
    <!-- 二维码 END -->
    
    <!-- No Comment -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
  <aside class="catalog-container">
  <div class="toc-main">
    <strong class="toc-title">Catalog</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#springboot-注解"><span class="toc-nav-text">springboot 注解</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'https://fengzhaoy.github.io/2019/05/24/SpringBoot2/springboot注解/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function () {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function () {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="' + src +
          '" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function () {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


<script>
  var qr = new QRious({
    element: document.getElementById('share-qrcode'),
    value: document.location.href
  });
</script>





    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
  <p class="copyright">
    &copy; 2019
  </p>
</footer>

<script>
  function async (u, c) {
    var d = document,
      t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) {
      o.addEventListener('load', function (e) {
        c(null, e);
      }, false);
    }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async ("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function () {
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async ("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function () {
    $('figure pre').each(function (i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>
<!-- Baidu Tongji -->

<script src="/js/script.js"></script>
  </body>
</html>